<template>
  <div v-loading="loading" element-loading-text="拼命加载中">
    <div>
      <el-input style="width: 200px" placeholder="填入项目名称(选填)" v-model="projectNameSearch"></el-input>
      <el-select v-model="creatorSearch" filterable placeholder="选择创建人" style="width: 150px; margin: 0 5px">
        <el-option v-for="item in userOptions" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>

      <el-select v-model="recipientSearch" filterable placeholder="选择接收人" style="width: 150px; margin: 0 5px">
        <el-option v-for="item in userOptions" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>

      <el-button type="primary" @click="load(1)">查询</el-button>
      <el-button type="success" @click="reset">刷新</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" plain @click="handleAddOrEdit">新增生产计划</el-button>
      <el-button type="danger" plain @click="handleDelBatch">批量删除</el-button>
    </div>
    <!--  整个表格，有多选框 -->
    <el-table v-loading="loading" element-loading-text="拼命加载中" :data="tableData" stripe :header-cell-style="{backgroundColor: 'aliceblue',color: '#666'}"
              @selection-change="handleSelectionChange">
      <!--    复选框-->
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="projectNum" label="项目编号" width="100px"></el-table-column>
      <el-table-column prop="projectName" label="项目名称" width="100" ></el-table-column>
      <el-table-column prop="projectTag" label="项目标签" width="100"></el-table-column>
      <el-table-column prop="projectDescription" label="项目简述" width="100" show-overflow-tooltip></el-table-column>
      <el-table-column prop="attachName" label="附件名称" width="100" show-overflow-tooltip></el-table-column>
      <el-table-column prop="recipientName" label="接收人" width="100"></el-table-column>
      <el-table-column prop="auditorName" label="审核人" width="100"></el-table-column>
<!--      <el-table-column prop="state" label="状态" width="80"></el-table-column>-->
      <el-table-column prop="state" label="当前状态(点击查记录)" width="180">
        <template v-slot="scope">
          <el-button @click="previewAuditRecord" :type="getStatusType(scope.row.state)">{{getStatusText(scope.row.state)}}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="expirationTime" label="到期时间" width="100"></el-table-column>
      <el-table-column prop="creatTime" label="创建时间" width="140"></el-table-column>
      <el-table-column prop="creatorName" label="创建人" width="100"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="140"></el-table-column>
      <el-table-column prop="modifierName" label="更新人" width="100"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="180px">
        <template v-slot="scope">
          <el-button v-if="allowEdit(scope.row)"size="mini" type="primary" plain @click="handleAddOrEdit(scope.row)">编辑</el-button>
<!--          <el-button v-if="isAudit(scope.row)" size="mini" type="info" plain @click="handleDel(scope.row)">删除</el-button>-->
          <el-button v-if="isAudit(scope.row)" size="mini" type="info" plain @click="openAudit(scope.row)">审核</el-button>
          <el-button v-if="isRecipient(scope.row)" size="mini" type="success" plain @click="handleConfirm(scope.row)">接收</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0;text-align: center">
      <el-pagination @current-change="handleCurrentChange"
                     :current-page="pageNum"
                     :page-size="pageSize"
                     layout="total,prev,pager,next"
                     :total="total">
      </el-pagination>
    </div>

    <!-- 新增表单需要弹窗包裹 / close-on-click-model点击弹窗外不会关闭-->
    <el-dialog :title="editTitle"
               :visible.sync="formVisible"
               width="40%"
               :close-on-click-model="false">
      <el-form :model="form"
               label-width="80px"
               style="padding-right: 20px"
               :rules="rules"
               ref="formRef">
        <el-form-item label="合同号" prop="contractNum">
          <el-input v-model="form.contractNum" placeholder="如：HT1001"></el-input>
        </el-form-item>

        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="如：张三"></el-input>
        </el-form-item>

        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="如：金刚石项目"></el-input>
        </el-form-item>

        <el-form-item label="开始时间" prop="startTime">
          <template>
            <div class="block">
              <span class="demonstration"></span>
              <el-date-picker
                  v-model="form.startTime"
                  type="date"
                  placeholder="选择开始时间">
              </el-date-picker>
            </div>
          </template>
        </el-form-item>

        <el-form-item label="结束时间" prop="endTime">
          <template>
            <div class="block">
              <span class="demonstration"></span>
              <el-date-picker
                  v-model="form.endTime"
                  type="date"
                  placeholder="选择结束时间">
              </el-date-picker>
            </div>
          </template>
        </el-form-item>

        <el-form-item label="合同期限" prop="contractTerm">
          <el-input v-model="form.contractTerm" placeholder="如：15个工作日"></el-input>
        </el-form-item>

        <el-form-item label="有无合同" prop="hasContract">
          <el-radio-group v-model="form.hasContract">
            <el-radio label="1">有</el-radio>
            <el-radio label="0">无</el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- prop 与rules字段绑定     -->
        <el-form-item label="项目介绍" prop="projectDescription">
          <el-input type="textarea" v-model="form.projectDescription" placeholder="选填，简单描述这个项目"></el-input>
        </el-form-item>

        <el-form-item label="附件" prop="attachName">
          <el-input v-model="form.attachName" placeholder="暂无附件" :readonly="true"></el-input>
          <div style="margin: 15px;text-align: center">
            <el-upload ref="uploader" class="upload-demo" :action="this.$apiUrl+'/file/upload'" :headers="{token: user.token}" :file-list="fileList" :on-preview="handlePreview" :on-success="handleOnSuccess" :before-upload="beforeUpload" :before-remove="beforeRemoved" multiple :limit="3" :on-exceed="handleExceed">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">注意：单个附件安全范围是100MB以内</div>
            </el-upload>
          </div>
        </el-form-item>

        <!-- <el-form-item label="接收人" prop="recipient">
          <el-select v-model="form.recipient" filterable placeholder="选填" :options="userOptions" value-key="value" style="width: 150px; margin: 0 5px">
            <el-option v-for="item in userOptions" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item> -->

<!--        <el-form-item label="审核人" prop="content">-->
<!--          <el-input v-model="form.auditorName" placeholder="默认赵总"></el-input>-->
<!--        </el-form-item>-->

        <!-- <el-form-item label="到期时间" prop="expirationTime">
          <template>
            <div class="block">
              <span class="demonstration"></span>
              <el-date-picker
                  v-model="form.expirationTime"
                  type="date"
                  placeholder="选择日期">
              </el-date-picker>
            </div>
          </template>
        </el-form-item> -->

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核意见" :visible.sync="auditVisible" width="40%">
      <div style="margin: 0 auto;text-align: center">
        <span style="font-weight: bold;font-size: 20px">请检查以下待审核信息：</span>
      </div>
      <template>
        <div style="margin-top: 20px; margin-bottom: 20px;">
          <el-radio-group v-model="radioSize">
<!--            <el-radio label="">默认</el-radio>-->
            <el-radio label="medium">大</el-radio>
            <el-radio label="small">中</el-radio>
            <el-radio label="mini">小</el-radio>
          </el-radio-group>
        </div>

        <el-descriptions :column="2" :size="radioSize" border>
<!--          <template slot="extra">-->
<!--            <el-button type="primary" size="small">操作</el-button>-->
<!--          </template>-->
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              项目编号
            </template>
            {{auditForm.projectNum}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-mobile-phone"></i>
              项目名称
            </template>
            {{auditForm.projectName}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-location-outline"></i>
              项目标签
            </template>
            {{auditForm.projectTag}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-tickets"></i>
              项目简述
            </template>
            {{auditForm.projectDescription}}
<!--            <el-tag size="small">学校</el-tag>-->
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              附件
            </template>
            <el-upload ref="uploader" class="upload-demo" :action="this.$apiUrl+'/file/upload'" :file-list="fileList" :on-preview="handlePreview" :before-remove="beforeRemovedAudit" multiple :limit="30">
            </el-upload>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              接收人
            </template>
            {{auditForm.recipientName}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              到期时间
            </template>
            {{auditForm.expirationTime}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              备注
            </template>
            {{auditForm.remark}}
          </el-descriptions-item>
        </el-descriptions>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditVisible=false">关闭</el-button>
        <el-button type="danger" plain @click="reject">拒绝</el-button>
        <el-button type="success" plain @click="pass">同意</el-button>
      </span>
    </el-dialog>
    <!-- 预览图片 -->
    <el-dialog :visible.sync="dialogVisible">
      <img width="60%" :src="dialogImageUrl">
    </el-dialog>

    <el-dialog title="审核记录" :visible.sync="auditRecordVisible" width="40%">
      <el-table v-loading="loading" element-loading-text="拼命加载中" :data="tableData" stripe :header-cell-style="{backgroundColor: 'aliceblue',color: '#666'}"
                @selection-change="handleSelectionChange">
        <!--    复选框-->
        <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
        <el-table-column prop="projectNum" label="项目编号" width="100px"></el-table-column>
        <el-table-column prop="projectName" label="项目名称" width="100" ></el-table-column>
        <el-table-column prop="projectTag" label="项目标签" width="100"></el-table-column>
        <el-table-column prop="projectDescription" label="项目简述" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column prop="attachName" label="附件名称" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column prop="recipientName" label="接收人" width="100"></el-table-column>
        <el-table-column prop="auditorName" label="审核人" width="100"></el-table-column>
        <el-table-column prop="expirationTime" label="到期时间" width="100"></el-table-column>
        <el-table-column prop="creatTime" label="创建时间" width="140"></el-table-column>
        <el-table-column prop="creatorName" label="创建人" width="100"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="140"></el-table-column>
        <el-table-column prop="modifierName" label="更新人" width="100"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>

      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditRecordVisible=false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "ProPlan",
  data(){
    return {
      tableData: []//表数据
      ,loading: true
      ,pageNum: 1//当前页码
      ,pageSize: 15//每页数量
      ,projectNameSearch: ''
      ,creatorSearch: ''
      ,recipientSearch: ''
      ,content: ''
      ,total: 0 //数据总条数
      ,formVisible: false //默认不显示新增表单弹窗
      ,auditVisible: false //默认不显示审核意见弹窗
      ,auditRecordVisible: false //默认不显示审核记录弹窗
      ,form: {}
      ,auditForm:{}
      ,user: JSON.parse(localStorage.getItem('userInfo') || '{}')
      ,rules: {
        projectNum: [
          {required: true,message: '输入项目编号',trigger: 'blur'}
        ],
        projectName: [
          {required: true,message: '输入项目名称',trigger: 'blur'}
        ],
        recipient: [
          {required: true,message: '选择接收人',trigger: 'blur'}
        ]
      }
      ,ids: []
      ,userOptions: []
      ,expirationTime: '' //到期时间
      ,auditSate: [1,3,5] //正在审核的状态码
      ,auditors: [44,46] //有权审核的人
      ,editTitle: '编辑信息'
      ,fileList: []
      ,attachIds: []
      ,radioSize: ''
      ,dialogImageUrl: '' //图片预览对话框图片地址
      ,dialogVisible: false//图片预览对话框
    }
  },
  created() {
    this.load();
  },
  //安装
  mounted() {
    this.initUsersOptions()
  },
  methods: {
    //上传文件组件，限制附件个数
    handleExceed(files, fileList) {
      this.$message.warning(
          `当前限制选择 3 个附件，本次选择了 ${
              files.length
          } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },
    //上传文件组件，成功后记录文件名和文件路径
    handleOnSuccess(response, file, fileList) {
      // console.log(response);
      this.fileList = fileList
      this.attachIds.push(response.data.id)
      //改变附件名称
      this.updateAttachNameAndId()
    },
    //更新附件名称和附件ID
    updateAttachNameAndId(){
      this.form.attachName = this.fileList
          .map((file) => file.name)
          .join('，')
      this.form.attachIds = this.attachIds.map(id => `${id}`).join('，');
    },
    //上传组件，移除元素之前
    beforeRemoved(file, fileList) {
      if (file && file.status === 'success') {
        this.$confirm(`确定移除【${file.name}】吗？`)
            .then(() => {
              //刷新一下全局变量
              this.fileList = fileList
              if (this.fileList.length < 1){
                this.attachIds = []; //重置一下
              }
              this.attachIds = this.attachIds.filter(i => i !== file.response.data.id);
              //确认移除,改变附件名称和附件ID
              this.updateAttachNameAndId()
              return true
            })
            .catch(() => {
              //取消或关闭
              return false
            })
      }
    },
    //审核预览附件，点击移除时
    beforeRemovedAudit(file, fileList) {
      // this.$message.warning("当前是审核页，移除附件请到编辑页操作，本次移除动作不做保留。若需改动系统设计请联系林硕修改")
      this.$notify({
        title: '系统功能讲解：',
        message: '你若是想预览文件，请点击文件名称即可预览。你若是想移除这个文件，请到编辑页操作移除。本次移除的文件仅在当前审核页消失，实际上系统没有移除这个文件，你再次打开还能看到。若需改动系统设计请联系林硕修改。',
        duration: 0
      })
    },
    //上传文件之前
    beforeUpload(file) {
      let size = file.size
      let maxSize = 100 * 1024 * 1024
      if (size > maxSize) {
        // this.$message.warning("附件大小不能超过100MB")
        // return false
        // this.$refs.uploader.abort() //终止上传
        return this.$confirm(
            '当前附件【' +
            file.name +
            '】文件大小超出100MB安全范围，继续上传可能会出错，请确认是否继续上传？',
            '警告提示'
        )
      }
    },
    //上传文件组件，文件预览
    handlePreview(file) {
      const imageExtensions = [
        'jpg',
        'jpeg',
        'png',
        'gif',
        'bmp',
        'tiff',
        'webp',
      ]
      let fileExtensionMatch = file.response.data.fileName.match(/\.([^\.]+)$/)
      let fileExtension = fileExtensionMatch ? fileExtensionMatch[1] : '' //文件扩展名
      if (imageExtensions.includes(fileExtension.toLowerCase())) {
        // this.$message.warning('预览图片功能正在开发中……')
        this.dialogImageUrl = file.response.data.fileUrl;
        this.dialogVisible = true;
      } else {
        this.$message.warning('选中的文件不是图片，暂不支持预览')
      }
    },
    reject(){
      this.$confirm('为防止误触，确认要拒绝吗？','确认拒绝',{type: "warning"}).then(ok => {
        switch (this.auditForm.state){
          case 1:
            this.auditForm.state = 21;
            break;
          case 3 :
            this.auditForm.state = 41;
            break;
          case 5 :
            this.auditForm.state = 61;
            break;
          default:
            this.$message.warning("处理状态流转异常！");
            return;
        }
        this.sendAuditRequest('audit_reject');
      }).catch(() => {}) //catch捕捉先不做处理
    },
    pass(){
      this.$confirm('为防止误触，确认要通过吗？','确认拒绝',{type: "warning"}).then(ok => {
        switch (this.auditForm.state){
          case 1:
            this.auditForm.state = 2;
            break;
          case 3 :
            this.auditForm.state = 4;
            break;
          case 5 :
            this.auditForm.state = 6;
            break;
          default:
            this.$message.warning("处理状态流转异常！");
            break;
        }
        this.sendAuditRequest('audit_pass');
      }).catch(() => {}) //catch捕捉先不做处理
    },
    //发送审核结果请求
    sendAuditRequest(audit){
      this.$request({
        //有id就是编辑
        method: 'PUT',
        data: this.auditForm,
        url: '/project/'+audit
      }).then(res => {
        if (res.code === '200'){
          //保存成功
          this.$message.success('保存成功')
          this.auditVisible=false;
        }else {
          this.$message.error(res.msg) //错误信息
        }
      })
    },
    reset(){
      this.projectName = ''
      this.creator = ''
      this.recipient = ''
      this.load()
    },
    load(pageNum){
      this.loading = true;
      if (pageNum) this.pageNum = pageNum;  //分页查询时自动切换到第一页
      this.$request.get('/project/findByPage',{
        params:{
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          projectName: this.projectNameSearch,
          creator: this.projectNameSearch,
          recipient: this.projectNameSearch
        }
      }).then(res => {
        if (res.data){
          if (res.data.records){
            // this.tableData = res.data
            this.tableData = res.data.records  //若records不显示可替换为List
          }else{
            this.$message.warning("res.data.records不存在")
          }
          this.total = res.data.total
        }else {
          this.$message.error("接口未返回数据，请联系管理员!")
          // this.$router.push('/login')
        }
        this.loading = false
      })
    },
    //切换页码
    handleCurrentChange(pageNum){
      // this.pageNum = pageNum //方法传递过来参数就不用赋值了
      this.load(pageNum)
    },
    //新增弹窗事件||编辑弹窗
    //注意：row无论在哪种模式，永远不为空，只是值内容多少的区别，所以同函数无法精确区分两种模式，要么分函数，要么根据内容来区分
    handleAddOrEdit(row){
      // this.form = row.id ?  JSON.parse(JSON.stringify(row)) : {}
      //判断是否编辑模式，默认false
      if (row.id){
        this.isEditMode = true;
        this.form = JSON.parse(JSON.stringify(row)) //弹窗带出来信息,浅拷贝是内存指向，深拷贝是开区域赋值
        //编辑比较多样，所以标题变为动态
        this.editTitle = this.getStatusText(row.state)
        //需要把upload组件的fileList带出来
        if (this.form.fileListJson) {
          this.fileList = this.form.fileListJson
        } else {
          this.fileList = []
        }
      }else {
        this.isEditMode = false; //其实不用写，默认就是false
        this.form = {} //新增数据时清空表单
      }
      this.formVisible = true //显示弹窗
    },
    //预览审核记录
    previewAuditRecord(){
      this.auditRecordVisible = true;
    },
    updateState(row){
      if (row.id){

      }
    },

    //保存之前状态流转修改
    beforeSaveUpdateState(){
      switch (this.form.state){
        case 2: //待起草 --> 待初步工程图审核
          this.form.state = 3;
          break;
        case 21: //待修改立项 - 待立项审核
          this.form.state = 1;
          break;
        case 4 : //待细化 - 待细化审核
          this.form.state = 5;
          break;
        case 41 : //待修改工程图 - 待工程图审核
          this.form.state = 3;
          break;
        case 61 : //待修改细化工程图 - 待细化工程图审核
          this.form.state = 5;
          break;
        case 7 : //已接收 - 待初步工程图审核
          this.form.state = 3;
          break;
        default: //无论是新增还是编辑，初始状态都是1，但若编辑前已经有进行中的其他状态，则顺延状态。
          this.form.state = 1;
          break;
      }
    },
    //发送save请求
    sendSave(){
      this.beforeSaveUpdateState();
      //发送请求
      this.$request({
        //有id就是编辑
        method: this.form.id ? 'PUT' : 'POST',
        data: this.form,
        url: this.form.id ? '/project/update' : '/project/save'
      }).then(res => {
        if (res.code === '200'){
          //保存成功
          this.$message.success('保存成功')
          this.formVisible = false;
          this.load(this.pageNum)
        }else {
          this.$message.error(res.msg) //错误信息
          return false
        }
        //无论是否保存成功，都要把fileList清空
        this.fileList = []
      })
    },
    //新增保存
    save(){
      //校验表单
      this.$refs.formRef.validate((valid) => {
        if (valid){
          this.form.fileList = this.fileList;
          this.sendSave()
        }else {
          this.$message.warning("表单校验未通过")
        }
      })
    },
    //打开审核弹窗
    openAudit(row){
      this.auditVisible = true
      this.auditForm = row
      if (row.fileListJson){
        this.fileList = row.fileListJson
      }else {
        this.fileList = []
      }
    },
    //单个删除
    handleDel(row){
      this.$confirm('确认删除《'+row.projectName+'》吗？','确认删除',{type: "warning"}).then(ok => {
        this.$request.delete('/project/delete/'+row.id).then(res => {
          if (res.code == '200'){
            this.$message.success("删除成功")
          }else {
            this.$message.error("删除失败："+res.msg)
          }
          this.load(this.pageNum) //刷新表格
        })
      }).catch(() => {}) //catch捕捉先不做处理
    },
    //批量删除之前--多选框，参数是选中的行对象
    handleSelectionChange(rows){
      this.ids = rows.map(row => row.id) //流和表达式，将行id提取出来存入数组
    },
    //批量删除
    handleDelBatch(){
      //校验：点击批量删除按钮之前，未选择多选框时提示
      if (!this.ids.length){
        this.$message.warning('请选择需要批量删除的记录！')
        return
      }
      this.$confirm('你确认批量删除['+this.ids+']这些记录吗？','确认删除',{type: "warning"}).then(ok => {
        this.$request.delete('/project/deleteBatch',{data: this.ids}).then(res => {
          if (res.code == '200'){
            this.$message.success("批量删除成功")
          }else {
            this.$message.error("批量删除失败:"+res.msg)
          }
          this.load(this.pageNum) //刷新表格
        })
      }).catch(() => {}) //catch捕捉先不做处理
    },
    //是否公开 改变事件
    openChange(form){
      // this.form = form; //这样不合适，是属于直接指向内存地址，浅拷贝会直接改变影响数据库原本数据
      this.form = JSON.parse(JSON.stringify(form))  //变为与行对象没有关系，而是重新赋值，新建自己的内存空间，互不影响，属于深拷贝
      //调用更新，无需校验，直接发请求
      this.sendSave()
    },

    getStatusText(status) {
      switch (status) {
        case 1:
          return '待立项审核';
        case 2:
          return '立项审核通过 待起草';
        case 21:
          return '立项审核失败 待修改';
        case 3:
          return '待初步工程图审核';
        case 4:
          return '审核通过 待细化工程图';
        case 41:
          return '审核失败 待修改初步工程图';
        case 5:
          return '待细化工程图审核';
        case 6:
          return '审核通过 待负责人接收';
        case 61:
          return '审核失败 待修改细化工程图';
        case 7:
          return '已接收 有效期倒计时开始';
        case 8:
          return '图纸修改';
        case 99:
          return '已过期';
        default:
          return '未知状态';
      }
    },
    getStatusType(status) {
      switch (status) {
        case 1:
          return 'info';
        case 2:
          return 'success';
        case 21:
          return 'danger';
        case 3:
          return 'info';
        case 4:
          return 'success';
        case 41:
          return 'danger';
        case 5:
          return 'info';
        case 6:
          return 'success';
        case 61:
          return 'danger';
        case 7:
          return 'primary';
        case 8:
          return 'waring';
        case 99:
          return 'danger';
        default:
          return 'info'; // 默认没有特殊样式
      }
    },
    //向接口发送
    sendApi(){
      this.$request({
        //有id就是编辑
        method: this.form.id ? 'PUT' : 'POST',
        data: this.form,
        url: this.form.id ? '/project/update' : '/project/save',
      }).then((res) => {
        if (res.code === '200') {
          //保存成功
          // this.$message.success('操作成功')
          this.formVisible = false
          this.load(this.pageNum)
        } else {
          this.$message.error(res.msg) //错误信息
          return false
        }
        //无论是否保存成功，都要把fileList清空
        this.fileList = []
      })
    },
    //是否是接收人,并且接收状态是未接收
    isRecipient(row){
      return (row.state === 6) && (row.recipient === this.user.id);
    },
    //是否是审核人
    isAudit(row){
      //非审核人不可见，且状态是待审核
      // return this.auditors.includes(this.user.id);
      // return (this.auditors.includes(this.user.id)) && (this.auditSate.includes(row.state));
      return (this.user.id===row.auditor) && (this.auditSate.includes(row.state));
    },
    //是否允许编辑
    allowEdit(row){
      //审核状态下不可编辑,待接收不可编辑
      return (!this.auditSate.includes(row.state) && row.state !== 6);
      // return (!this.auditSate.includes(row.state)) && (row.creator===this.user.id);
    },
    //确认接收
    handleConfirm(row){
      this.$confirm('你确认接收《'+row.projectName+'》这个项目吗？','确认',{type: "warning"}).then(ok => {
        if (this.isRecipient(row)){
          this.form = row
          this.form.state = 7
          this.sendApi();
          this.$message.success("你已接收成功")
        }else {
          this.$message.error('你不是《'+row.projectName+'》项目的接收人，请核对');
        }
      }).catch(() => {}) //catch捕捉先不做处理
    },
    //初始化用户选择框
    initUsersOptions() {
      this.$request({
        method: 'GET',
        url: '/user/getSelect',
      }).then((res) => {
        if (res.code === '200') {
          this.userOptions = res.data
        } else {
          this.$message.error(res.msg) //错误信息
          return false
        }
      })
    },
  }
}
</script>

<style>
</style>